<?php


namespace app\common\service\order\coupon_order\jutuike_channel;


use api\channel\jutuike\Coupon;
use api\channel\jutuike\TravelUnion;
use app\common\service\order\coupon_order\Base;
use app\common\service\order\coupon_order\CouponOrderChannelInterface;
use app\model\Agent;
use app\model\AgentServiceConfig;
use app\model\BaseSupplier;
use app\model\CouponOrder;
use think\db\Query;
use think\Exception;
use think\facade\Db;


/**
 * Class JutuikeChannelBase
 * @package app\common\service\order\coupon_order\jutuike_channel
 */
abstract class JutuikeChannelBase extends Base implements CouponOrderChannelInterface
{

    /**
     * 创建时间
     */
    public function getOrderCreatedAt()
    {
        return $this->params['create_time'] ?? '';
    }

    /**
     * 第三方供应商标识id
     */
    public function getSupplierOrderSn()
    {

        if (empty($this->params['order_sn'])) {
            throw new Exception('缺少第三方供应商的订单号');
        }

        return $this->params['order_sn'];
    }
    /**
     * 退款金额
     */
    public function getRefundAmount()
    {
        return 0;
    }

    /**
     * 获取第三方订单号
     */
    public function getOutOrderId()
    {
        if (!empty($this->params['extension']['trade_parent_id'])) {
            return $this->params['extension']['trade_parent_id'];
        }
        return $this->getSupplierOrderSn();
    }


    public function getRefundStatus()
    {
        return CouponOrder::C_REFUND_STATUS_NO;
    }


    /**
     * 获取订单金额
     */
    public function getAmount()
    {

        if (!array_key_exists('pay_price',$this->params)) {
            throw new Exception('没有' . 'pay_price' . '字段');
        }
        return yuan2fen($this->params['pay_price']);

    }


    /**
     * 这笔订单所有的佣金
     */
    public function getOrderAllCommission()
    {
        return $this->params['jtk_share_fee'] ?? 0;
    }


    /**
     * 代理id
     */
    public function getAgentId()
    {
        $msg = '代理商不存在，过滤了';

        if (empty($this->_agent_id)) {
            throw new Exception($msg);
        };

        if (!Agent::find($this->_agent_id)) {

            echo $msg . PHP_EOL;
            \think\facade\Log::info($msg);
            throw new Exception($msg);
        };

        return $this->_agent_id;
    }

    /**
     * 获取原价 比如 qq会员30块，原销售价 有得话加，没有就算了
     */
    public function getOriginAmount()
    {
        return '';
    }

    /**
     * 用户支付的金额
     */
    public function getPayAmount()
    {

        return $this->amount;
    }

    /**
     * 支付状态
     */
    public function getPayStatus()
    {
        //同步的都是已支付
        return CouponOrder::C_PAY_STATUS_YES;

    }

    /**
     * 支付时间
     */
    public function getPayTime()
    {


        if (empty($this->params['pay_time']) && $this->params['status'] != 4) {
            throw new Exception('接口缺少支付时间');
        }

        return $this->params['pay_time'] ?? null;
    }

    /**
     * 用户id
     */
    public function getUserId()
    {

        return $this->_user_id;

    }

    public function getStatusDesc()
    {
        return $this->params['status_desc'] ?? '待结算';
    }

    /**
     * 结算时间
     */
    public function getSettledAt()
    {
        $pay_time = $this->getPayTime();
        if ($pay_time) {
            return $this->getSettleDate($this->getPayTime());
        }
        return null;

    }


    /**
     * 结算
     * @return mixed
     */
    public function getType()
    {
        return 0;
    }

    /**
     * 获取渠道
     */
    public function getCnid()
    {

        return $this->_channel_id;
    }


    /**
     * 聚推客都是返佣的方式
     * @return int
     */
    public function getSettleType()
    {
        return SETTLE_TYPE_PROFIT;
    }


    /**
     * 同步订单
     * @return mixed
     */
    abstract public function syncOrder();


    /**
     * 类型 常量类型
     * @return mixed
     */
    abstract public function getServType();

    /**
     * 产品名称 可以是title
     * @return mixed
     */
    abstract public function getProductName();

    /**
     * 产品图标
     * @return mixed
     */
    abstract public function getProductImage();


    /**
     *  获取结算状态
     * @return mixed
     */
    abstract public function getAgentSettleStatus();

    /**
     * 获取状态
     * @return mixed
     */
    abstract public function getStatus();


    /**
     * 获取渠道标识
     */
    public function getSid()
    {
        return $this->params['sid'];
    }

    public function getTitle()
    {
        return $this->params['act_name'] ?? '';
    }

    /**
     * 扩展字段
     */
    public function getExtData()
    {
//        if (!empty($this->params['extension'])) {
//            return $this->params['extension'];
//        }
        return $this->params;
    }


    /**
     * 设置聚推客的第三方
     * @param \api\channel\jutuike\Base $extend
     */
    public function setExtendApi($extend)
    {
        $this->extend = $extend;
        return $this;
    }


}